import { useUserStore } from "@/stores/user";
import axios, { type AxiosResponse, type InternalAxiosRequestConfig } from "axios";

export interface ResultType {
    code: number;
    msg: string;
    data: any;
}

export const service = axios.create({
    baseURL: 'http://localhost:8080',
    timeout: 5000,
})

service.interceptors.request.use((config) => {
    const userStore = useUserStore();
    if (userStore.token) {
        config.headers.set("token", userStore.token);
    }
    return config;
}, (e) => {
    console.log(e);
})

// 响应拦截器
service.interceptors.response.use(
    (response) => {
        const { data } = response;
        return data;
    },
    (error) => {
        console.log(error);
        return Promise.reject(error);
    }
);
